<?php
class Problem extends AppModel{
	var $name = 'Problem';
	var $hasMany=array("Scoreboard");
	var $answer_time=125;
	/*
	 *
	 * activate ($id) - Activate problem.
	 *
	 * param:
	 *
	 * $id : problem id
	 *
	 * return:
	 *
	 * true if success, false if fail.
	 *
	 */
	function open($id){
		App::import('Helper', 'Time');
		$time = new TimeHelper();
		$this->id=$id;
		$data=array("Problem"=>array("open"=>1,"open_time"=>date('Y-m-d H:i:s'),"close_time"=>date('Y-m-d H:i:s',time()+$this->answer_time)));
		if(!$this->save($data)){
			throw new Exception("Save failed.");
		}
	}
	function close($id){
		App::import('Helper', 'Time');
		$time = new TimeHelper();
		$this->id=$id;
		$data=array("Problem"=>array("open"=>2,"close_time"=>date('Y-m-d H:i:s')));
		if(!$this->save($data)){
			throw new Exception("Save failed.");
		}
		return true;
	}
	function hide($id,$silent=false){
		App::import('Helper', 'Time');
		$time = new TimeHelper();
		$this->id=$id;
		$data=array("Problem"=>array("open"=>0));
		if(!$this->save($data)){
			throw new Exception("Save failed.");
		}
		return true;
	}
	function refresh(){
		$problems=$this->find('all',array('conditions'=>array('open'=>1,'`Problem`.`close_time` <='=>date('Y-m-d H:i:s',$GLOBALS['now']))));
		foreach($problems as $k=>$problem){
			$this->close($problem['Problem']['id'],true);
		}
		return true;
	}
}
?>
